<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="头像" />
      </div>
      <!-- 表单区域 -->
      <!-- rules放form标签里面 -->
      <el-form ref='loginFormRef' label-width="0px" class='login_form' :model="form" :rules='loginRules' status-icon>
        <!-- 用户名 -->
        <!-- 验证属性要放在父级上 -->
        <!-- 这里面prop需要和v-model保持名称一致，否则会有问题 -->
        <el-form-item prop='userName'>
          <el-input prefix-icon="iconfont icon-user" v-model="form.userName" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop='password'>
          <el-input prefix-icon="iconfont icon-password" v-model="form.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="onSubmit(form.userName)">登录</el-button>
          <el-button type="info" @click='reset'>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '@/utils/https.js'
export default {
  name: 'login',
  data () {
    return {
      inputValue: '',
      results: null,
      // 这是表单动态绑定的对象
      form: {
        userName: 'admin',
        password: '123456'
      },
      loginRules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '名称长度在3-15个字之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '名称长度在6-12个字之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    onSubmit (inputValue) {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return
        login().then(async results => {
          const { data: res } = await results
          if (res.data.status !== 200) return this.$message.error('登陆失败！')
          this.$message.success('登陆成功！')
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
        })
      })
    },
    reset () {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: black;
  height: 100%;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
  .avatar_box {
    height: 130px;
    width: 130px;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
          background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: black;
    }
  }
  .login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
      box-sizing: border-box;
        .btns{
    display: flex;
    justify-content: end;
  }
  }
}
</style>
